package com.crazypos.common;

import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Slf4j
@NoArgsConstructor
public class SecurityResponse {

    public static <T> void reponseJson(HttpServletResponse response, ApiRestResponse data) {
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            response.setStatus(data.getStatus());
            out = response.getWriter();
            out.println(JSON.toJSONString(data));
        } catch (Exception e) {
            log.error("【JSON输出异常】" + e);
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    }

}
